package com.iwhalecloud.uncc.dao.impl;

import com.iwhalecloud.uncc.common.ArchiverConstant;
import com.iwhalecloud.uncc.common.ArchiverThreadLocal;
import com.iwhalecloud.uncc.dao.ArchiverDao;
import com.iwhalecloud.uncc.db.DatasourceContextHolder;
import com.iwhalecloud.uncc.domain.ArchiveParam;
import com.iwhalecloud.uncc.model.MoniBrStatInfo;
import com.iwhalecloud.uncc.utils.BatchUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author zq
 * @date 2021/10/26 23:33
 * @description
 */
@Repository("moniBrStatInfoArchiverDao")
public class MoniBrStatInfoArchiverDaoImpl implements ArchiverDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Object query(Object o) {
        return null;
    }

    @Override
    public List queryList(Object o) {
        StringBuilder sql = new StringBuilder("SELECT * FROM MONI_BR_STAT_INFO WHERE COLL_SERVICE_ID = ? AND CREATE_TIME BETWEEN ? AND ? AND ARCHIVER_FLAG = " + ArchiverConstant.ONLINE);
        ArchiveParam aP = ArchiverThreadLocal.getArchiveParam();
        List<MoniBrStatInfo> list = jdbcTemplate.query(sql.toString(), new Object[]{o,aP.getStartDate(),aP.getEndDate()} , new BeanPropertyRowMapper<>(MoniBrStatInfo.class));
        if (!list.isEmpty()) {
            return list;
        }
        return null;
    }

    @Override
    public List batchQuery(List list) {
        StringBuilder sql = new StringBuilder("SELECT");
        sql.append(" `ID`, `P_CATALOG_ID`, `CATALOG_ID`, `NE_TYPE_ID`, `NE_SUB_TYPE_ID`, `COLLECT_CHANNEL_TYPE`, `NE_ID`, `COLL_SERVICE_ID`, `SOURCE_ID`, `RESTYPE_ID`, `MONI_INDEX_ID`, `MONI_INDEX_VALUE`, `MONI_INDEX_STATUS`, `NEED_ALARM`, `CREATE_TIME`, `PARTITION_CODE`, `STATE`");
        sql.append(" FROM MONI_BR_STAT_INFO WHERE COLL_SERVICE_ID in (:i)");
        ArchiveParam aP = ArchiverThreadLocal.getArchiveParam();
        int limit = (list.size())*(aP.getEachInsLines());
        sql.append(" AND CREATE_TIME BETWEEN \"" + aP.getStartDate() + "\" AND \"" + aP.getEndDate()+"\"");
        sql.append(" AND ARCHIVER_FLAG = " + ArchiverConstant.ONLINE);
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        sql.append(" LIMIT " + limit);
        List<MoniBrStatInfo> res = BatchUtil.batchQueryBeans(sql,list,new RowMapper() {
            @Override
            public Object mapRow(ResultSet resultSet, int i) throws SQLException {
                MoniBrStatInfo moniBrStatInfo = new MoniBrStatInfo();
                moniBrStatInfo.setId(resultSet.getLong("ID"));
                moniBrStatInfo.setpCatalogId(resultSet.getInt("P_CATALOG_ID"));
                moniBrStatInfo.setCatalogId(resultSet.getInt("CATALOG_ID"));
                moniBrStatInfo.setNeTypeId(resultSet.getInt("NE_TYPE_ID"));
                moniBrStatInfo.setNeSubTypeId(resultSet.getInt("NE_SUB_TYPE_ID"));
                moniBrStatInfo.setCollectChannelType(resultSet.getString("COLLECT_CHANNEL_TYPE"));
                moniBrStatInfo.setNeId(resultSet.getInt("NE_ID"));
                moniBrStatInfo.setCollServiceId(resultSet.getInt("COLL_SERVICE_ID"));
                moniBrStatInfo.setSourceId(resultSet.getInt("SOURCE_ID"));
                moniBrStatInfo.setResTypeId(resultSet.getString("RES_TYPE_ID"));
                moniBrStatInfo.setMoniIndexId(resultSet.getInt("MONI_INDEX_ID"));
                moniBrStatInfo.setMoniIndexValue(resultSet.getString("MONI_INDEX_VALUE"));
                moniBrStatInfo.setMoniIndexStatus(resultSet.getString("MONI_INDEX_STATUS"));
                moniBrStatInfo.setNeedAlarm(resultSet.getString("NEED_ALARM"));
                moniBrStatInfo.setCreateTime(resultSet.getTimestamp("CREATE_TIME"));
                moniBrStatInfo.setPartitionCode(resultSet.getString("PARTITION_CODE"));
                moniBrStatInfo.setState(resultSet.getString("STATE"));
                return moniBrStatInfo;
            }
        });
        return res;
    }

    @Override
    public void insert(Object o) {

    }

    @Override
    public void batchInsert(List list) {
        StringBuilder sql = new StringBuilder("INSERT INTO `moni_br_stat_info`");
        sql.append(" (`ID`, `P_CATALOG_ID`, `CATALOG_ID`, `NE_TYPE_ID`, `NE_SUB_TYPE_ID`, `COLLECT_CHANNEL_TYPE`, `NE_ID`, `COLL_SERVICE_ID`, `SOURCE_ID`, `RESTYPE_ID`, `MONI_INDEX_ID`, `MONI_INDEX_VALUE`, `MONI_INDEX_STATUS`, `NEED_ALARM`, `CREATE_TIME`, `PARTITION_CODE`, `STATE`, `ARCHIVER_TIME`)");
        sql.append(" VALUES (:id,:pCatalogId,:catalogId,:neTypeId,:neSubTypeId,:collectChannelType,:neId,:collServiceId,:sourceId,:resTypeId,:moniIndexId,:moniIndexValue,:moniIndexStatus,:needAlarm,:createTime,:partitionCode,:state,:archiverTime)");
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void update(int flag, Object o) {

    }

    @Override
    public void batchUpdate(int flag, List list) {
        StringBuilder sql = new StringBuilder("UPDATE MONI_BR_STAT_INFO SET ARCHIVER_FLAG = " + flag + " WHERE ID = :id");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }

    @Override
    public void delete(Object o) {

    }

    @Override
    public void batchDelete(List list) {
        StringBuilder sql = new StringBuilder("DELETE FROM MONI_BR_STAT_INFO WHERE ID = :id");
        sql.append(" AND PARTITION_CODE = "+ DatasourceContextHolder.getDs());
        BatchUtil.batchUpdateBeans(sql,list);
    }
}
